<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch">
      <el-row :gutter="10">
        <el-col :span="4">
          <el-form-item prop="userName">
            <el-input
              v-model="queryParams.userName"
              placeholder="请输入申请人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="applyTime">
            <el-date-picker clearable
                            v-model="queryParams.applyTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择申请时间"
                            style="width: 100%;"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" width="50" align="center">
        <template v-slot="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="userName" />
<!--      <el-table-column label="申请说明" align="center" prop="applyDescribe" />-->
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="applyStatus" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.model_apply_status" :value="scope.row.applyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="授权状态" align="center" prop="authorizationStatus" >
        <template v-slot="scope">
          <dict-tag :options="dict.type.model_authorization_status" :value="scope.row.authorizationStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:apply:edit']"
            v-if="scope.row.authorizationStatus === '0'"
          >授权</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['model:apply:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改模型授权申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" center append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="申请人姓名" prop="userName">
          <el-input v-model="form.userName" readonly/>
        </el-form-item>
<!--        <el-form-item label="申请说明" prop="applyDescribe">-->
<!--          <el-input v-model="form.applyDescribe" readonly/>-->
<!--        </el-form-item>-->
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker  disabled
            v-model="form.applyTime"
            type="date"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" rows="5" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="passed" icon="el-icon-check">通 过</el-button>
        <el-button @click="fail" icon="el-icon-close">不通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addApply, delApply, getApply, listApply, updateApply,passedApply,failApply} from "@/api/model/apply";

export default {
  name: "Apply",
  dicts: ['model_apply_status','model_authorization_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模型授权申请表格数据
      applyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelBasicInfoId:undefined,
        userName: null,
        applyTime: null,
        applyStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.modelBasicInfoId = this.$route.params && this.$route.params.id;
    this.getList();
  },
  methods: {
    /** 查询模型授权申请列表 */
    getList() {
      this.loading = true;
      listApply(this.queryParams).then(response => {
        this.applyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        modelBasicInfoId: null,
        userId: null,
        userName: null,
        applyDescribe: null,
        applyTime: null,
        applyStatus: null,
        authorizationStatus: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模型授权申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "模型授权申请";
      });
    },
    passed(){
      passedApply(this.form).then(res=>{
        if(res.code === 200){
          this.$modal.msgSuccess("授权通过");
          this.getList();
          this.open = false;
        }
      })
    },
    fail(){
      failApply(this.form).then(res=>{
        if(res.code === 200){
          this.$modal.msgSuccess("授权不通过");
          this.getList();
          this.open = false;
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除模型授权申请编号为"' + ids + '"的数据项？').then(function() {
        return delApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('apply/apply/export', {
        ...this.queryParams
      }, `apply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
